package com.efast.dmom.mgmt.service.mes.qm.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectSaveBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMRepairRecordSaveBean;
import com.efast.dmom.mgmt.bean.mes.qm.RepairConsumptionSaveBean;
import com.efast.dmom.mgmt.dao.mes.qm.QMDefectRepairDao;
import com.efast.dmom.mgmt.entity.master.TrBasBomItem;
import com.efast.dmom.mgmt.service.mes.qm.IRepairService;

@Service
public class DefectRepairService implements IRepairService {

	@Autowired
	QMDefectRepairDao repairDao = null;
	
	@Override
	public ResultBean getConsumptionListByVin(String vin,String part) {
		ResultBean resultBean = new ResultBean(true," ");
		List<RepairConsumptionSaveBean> list = new ArrayList<RepairConsumptionSaveBean>();
		List<RepairConsumptionSaveBean> consumptionListByVin = repairDao.getConsumptionListByVin(vin,part);
		if (consumptionListByVin.size() == 0 || consumptionListByVin == null) {
			List<RepairConsumptionSaveBean> selectBomItemBypartNoversion = repairDao.selectBomItemBypartNoversion(vin,part);
			RepairConsumptionSaveBean repairConsumptionSaveBean = selectBomItemBypartNoversion.get(0);
			list.add(repairConsumptionSaveBean);
			if (repairConsumptionSaveBean.getSnVerifyType() == 1) {
				resultBean.setObject(list);
				resultBean.setMessage("关键件");
			} else if(repairConsumptionSaveBean.getSnVerifyType() == 2) {
				resultBean.setObject(list);
				resultBean.setMessage("标准件");
			}
			return resultBean;
		} else {
			resultBean.setObject(consumptionListByVin);
			resultBean.setMessage("关键件");
			return resultBean;
		}
	}

	@Override
	public void updateConsumptionSn(List<QMRepairRecordSaveBean> list) {
		
		repairDao.updateConsumptionSn(list);
	}

	public void closeRepairDefect(Integer id) {
        
		QMCheckDefectSaveBean saveBean = new QMCheckDefectSaveBean();
		saveBean.setLastUpdateTime(new Date());
		saveBean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		saveBean.setStatus("已关闭");
		saveBean.setId(id);
		repairDao.closeRepairDefect(saveBean);
	}
}
